//
//  FindPwdViewController.m
//  liangyi
//
//  Created by 张水 on 16/12/28.
//  Copyright © 2016年 yswh. All rights reserved.
//

#import "FindPwdViewController.h"
#import "ChangePwdViewController.h"
#import "AppHeader.h"
@interface FindPwdViewController ()

@property(nonatomic,strong) CustomTextField *findPwd;

@property(nonatomic,strong) UIButton *getYZM;

@end


@implementation FindPwdViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav];
    [self setupUI];
}

-(void)setNav{
     self.title = @"找回密码";
    self.navigationController.navigationBar.translucent = NO;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
     self.navigationController.navigationBar.translucent = NO;
}
-(void)setupUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.findPwd = [[CustomTextField alloc]init];
    [self.findPwd setLeftTitleFont:[UIFont systemFontOfSize:14]];
    self.findPwd.validator = [PhoneValidator new];
    self.findPwd.leftTitle = @"手机号码";
    self.findPwd.leftTitleColor = [UIColor ColorWithHex:@"#7d7d7d"];
    self.findPwd.placeholder = @"输入手机号码";
    [self.findPwd setValue:[UIColor ColorWithHex:@"#a5a5a5"] forKeyPath:@"_placeholderLabel.textColor"];
    [self.findPwd setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    self.findPwd.layer.cornerRadius = 23;
    self.findPwd.layer.borderColor = [UIColor ColorWithHex:@"#efefef"].CGColor;
    self.findPwd.layer.borderWidth = 2;
    [self.view addSubview:self.findPwd];
    [self.findPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(30);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@48);
    }];
    
    self.getYZM = [[UIButton alloc]init];
    [self.view addSubview:self.getYZM];
    [self.getYZM mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.findPwd.mas_bottom).offset(35);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@48);
    }];
    self.getYZM.layer.cornerRadius = 23;
    self.getYZM.backgroundColor = [UIColor ColorWithHex:@"#5AC3D0"];
    [self.getYZM setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.getYZM setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.getYZM.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.getYZM addTarget:self action:@selector(getYZMBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)getYZMBtnDidClick{
    
        
    if (![self.findPwd validate]){
        [CustomProgressHUD showErrorWithStatus:self.findPwd.validator.errorMessage];
    }else{
        NSString *url = [BASEURL stringByAppendingString:kURL_Sendsend];
        NSDictionary *dict = @{@"phone":[NSString stringWithFormat:@"%@",self.findPwd.text],
                               @"position":@"findpwd"};
        NSString *jsonString = [NSString dictionaryToJson:dict];
        NSString *AESString = [NSString AES128Encrypt:jsonString];
        NSDictionary *parameters = @{@"parameters":AESString};

        [AppNetwork requestWithPath:url parameters:parameters success:^(id responseObject) {
            NSLog(@"%@",responseObject);
            NSLog(@"校验了");
            NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
            NSInteger code = network.code.integerValue;
            if (code == 0) {
                ChangePwdViewController *VC= [ChangePwdViewController new];
                            VC.userPhoneNo = self.findPwd.text;
                NSLog(@"%@",self.findPwd.text);
                NSLog(@"%@",VC.userPhoneNo);
                [self.navigationController pushViewController:VC animated:YES];
            }else{
                self.findPwd.text = nil;
                [CustomProgressHUD showErrorWithStatus:network.errorDescription];
                
            }
        } failure:^(id error) {
            [CustomProgressHUD showErrorWithStatus:@"连接失败,请检查您的网络"];
        }];
}

        
}
    


@end
